﻿using System;
using Enums;
using XF.ExControls;
using XF.Common;
using System.Windows.Forms;

namespace KYBM
{
    public partial class FrmDialogMaterielList : XFFormEx
    {
        XF.BLL.Base_Materiel bll = new XF.BLL.Base_Materiel();
        public XF.Model.Base_Materiel Materiel { get; set; } = new XF.Model.Base_Materiel();

        public FrmDialogMaterielList()
        {
            InitializeComponent();
        }

        private void tsBtnSure_Click(object sender, EventArgs e)
        {
            xfDataGridView1.EndEdit();
            if(xfDataGridView1.SelectedRows.Count == 1)
            {
                Materiel.Id = zDataConverter.ToInt(xfDataGridView1.SelectedRows[0].Cells[ColId.Name].Value);
                Materiel.MaterielName = zDataConverter.ToString(xfDataGridView1.SelectedRows[0].Cells[ColCode.Name].Value);
                Materiel.MaterielModel = zDataConverter.ToString(xfDataGridView1.SelectedRows[0].Cells[ColName.Name].Value);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void FrmDialogMaterielList_Load(object sender, EventArgs e)
        {
            xfDataGridView1.AutoGenerateColumns = false;
            BindData();
        }

        private void BindData()
        {
            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(tsTbMateriel.Text.Trim()))
            {
                strWhere += " and (MaterielName like '%" + tsTbMateriel.Text.Trim() + "%' or MaterielModel like '%" + tsTbMateriel.Text.Trim() + "%')";
            }
            xfDataGridView1.DataSource = bll.GetList(strWhere).Tables[0];
        }

        private void tsBtnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void FrmDialogMaterielList_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    tsBtnSure_Click(this, EventArgs.Empty);
                    break;
                case Keys.Escape:
                    tsBtnCancel_Click(this, EventArgs.Empty);
                    break;
            }
        }
    }
}
